<template>
  <div>
    <el-form
      ref="editFormRef"
      :rules="rules"
      :model="editForm.data"
      label-width="140"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备厂家" prop="vendor">
            <el-select
              v-model="editForm.data.vendor"
              filterable
              style="width: 320px"
              placeholder="请选择设备厂家"
            >
              <el-option
                v-for="item in vendor.data"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编号" prop="code">
            <el-input
              v-model="editForm.data.code"
              disabled
              style="width: 320px"
              maxlength="30"
              placeholder="请输入设备编号"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="使用状态" prop="state">
            <el-select
              v-model="editForm.data.state"
              filterable
              style="width: 320px"
              placeholder="请选择使用状态"
            >
              <el-option
                v-for="item in state.data"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="设备经纬度"
            prop="longLatItude"
            style="margin-bottom: -6px !important"
          >
            <el-form-item prop="longitude">
              <el-input
                v-model="editForm.data.longitude"
                style="width: 150px; margin-right: 20px"
                placeholder="请输入路段经度"
              />
            </el-form-item>
            <el-form-item prop="latitude">
              <el-input
                v-model="editForm.data.latitude"
                style="width: 150px"
                placeholder="请输入路段纬度"
              />
            </el-form-item>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="IP地址">
            <el-input
              v-model="editForm.data.ip"
              style="width: 320px"
              placeholder="请输入IP地址"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="MAC地址">
            <el-input
              v-model="editForm.data.mac"
              style="width: 320px"
              placeholder="请输入MAC地址"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="版本号">
            <el-input
              v-model="editForm.data.firmwareVersion"
              style="width: 320px"
              placeholder="请输入版本号"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(editFormRef)"
              >取 消</el-button
            >
            <el-button class="confirm-btn" @click="submitForm(editFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import {
  getEquipmentSupplier,
  getDictionary,
} from "@/service/modules/publicModule";
import { getEditVideo } from "../request.js";

const props = defineProps({
  operationId: String,
});

const vendor = reactive({
  data: [],
});

const state = reactive({
  data: [],
});

const getData = async () => {
  await getEquipmentSupplier("4").then((res) => {
    vendor.data = res.data.result;
  });

  await getDictionary("device_use_state").then((res) => {
    state.data = res.data.result;
  });
};

getData();

const editFormRef = ref("");

const editForm = reactive({
  data: {
    id: props.operationId.id,
    longLatItude: 1,
    category: 2,
    vendor: props.operationId.vendorId,
    code: props.operationId.code,
    state: props.operationId.state,
    longitude: props.operationId.longitude,
    latitude: props.operationId.latitude,
    ip: props.operationId.ip,
    mac: props.operationId.mac,
    firmwareVersion: props.operationId.firmwareVersion,
  },
});

const rules = reactive({
  vendor: [{ required: true, message: "请选择设备厂家", trigger: "change" }],
  code: [{ required: true, message: "请输入设备编号", trigger: "blur" }],
  state: [{ required: true, message: "请选择使用状态", trigger: "change" }],
  longLatItude: [{ required: true, message: "", trigger: "blur" }],
  longitude: [{ required: true, message: "请输入路段经度", trigger: "blur" }],
  latitude: [{ required: true, message: "请输入路段纬度", trigger: "blur" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getEditVideo(editForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: `视频设备[${editForm.data.code}]修改信息成功`,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  editForm.data = {};
  formEl.resetFields();
  emits("operationDone");
};
const emits = defineEmits(["operationDone"]);
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>